package com.ruoyi.slideshow.controller;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.slideshow.domain.NexusSlideshow;
import com.ruoyi.slideshow.service.INexusSlideshowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 轮播图Controller
 * 
 * @author Afanda
 * @date 2025-04-15
 */
@RestController
@Slf4j
@RequestMapping("/NexusSphere/slideshow")
public class NexusSlideshowController extends BaseController
{
    @Autowired
    private INexusSlideshowService nexusSlideshowService;

    @Autowired
    private RedisCache redisCache;

    private static final String USER_SLIDESHOW_KEY = "slideshow";

    /**
     * 查询轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:slideshow:list')")
    @GetMapping("/list")
    public TableDataInfo list(NexusSlideshow nexusSlideshow)
    {
        startPage();
        List<NexusSlideshow> list = nexusSlideshowService.selectNexusSlideshowList(nexusSlideshow);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:slideshow:export')")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NexusSlideshow nexusSlideshow)
    {
        List<NexusSlideshow> list = nexusSlideshowService.selectNexusSlideshowList(nexusSlideshow);
        ExcelUtil<NexusSlideshow> util = new ExcelUtil<NexusSlideshow>(NexusSlideshow.class);
        util.exportExcel(response, list, "轮播图数据");
    }

    /**
     * 获取轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:slideshow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(nexusSlideshowService.selectNexusSlideshowById(id));
    }

    /**
     * 新增轮播图
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:slideshow:add')")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NexusSlideshow nexusSlideshow)
    {
        String key = USER_SLIDESHOW_KEY;
        redisCache.deleteObject(key);
        log.info("轮播图添加成功");
        return toAjax(nexusSlideshowService.insertNexusSlideshow(nexusSlideshow));
    }

    /**
     * 修改轮播图
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:slideshow:edit')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NexusSlideshow nexusSlideshow)
    {
        String key = USER_SLIDESHOW_KEY;
        redisCache.deleteObject(key);
        log.info("轮播图修改成功");
        return toAjax(nexusSlideshowService.updateNexusSlideshow(nexusSlideshow));
    }

    /**
     * 删除轮播图
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:slideshow:remove')")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        String key = USER_SLIDESHOW_KEY;
        redisCache.deleteObject(key);
        log.info("轮播图删除成功");
        return toAjax(nexusSlideshowService.deleteNexusSlideshowByIds(ids));
    }

    @GetMapping("/user/list")
    public AjaxResult userSelectList(){
        return success(nexusSlideshowService.userSelectList());
    }
}
